package org.maiyodemo.xmladapter;

import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.annotation.adapters.XmlAdapter;

import org.maiyodemo.entity.Cat;
import org.maiyodemo.xmladapter.StringCat.Entry;

/**
 * 该转换器负责完成StringCat与Map<String, Cat>的互相转换
 * 
 * @author maiYo
 * 
 */
public class StringCatXmlAdapter extends XmlAdapter<StringCat, Map<String, Cat>> {

	public Map<String, Cat> unmarshal(StringCat v) throws Exception {
		Map<String, Cat> result = new HashMap<>();
		for (Entry entry : v.getEntries()) {
			result.put(entry.getKey(), entry.getValue());
		}
		return result;
	}

	public StringCat marshal(Map<String, Cat> v) throws Exception {
		StringCat result = new StringCat();
		for (String key : v.keySet()) {
			result.getEntries().add(new Entry(key, v.get(key)));
		}
		return result;
	}

}
